package com.daluzy.chmanager.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.adapter.ViewPager2Adapter;
import com.daluzy.chmanager.fragment.ManagerFragment;
import com.daluzy.chmanager.fragment.MineFragment;
import com.daluzy.chmanager.fragment.StudentHomeFragment;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.radio_home)
    RadioButton radioHome;
    @BindView(R.id.radio_mine)
    RadioButton radioMine;
    @BindView(R.id.radio_group_bottom)
    RadioGroup radioGroupBottom;
    @BindView(R.id.view_pager)
    ViewPager2 viewPager;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private StudentHomeFragment studentHomeFragment;
    private MineFragment mineFragment;
    private ManagerFragment managerFragment;
    private ViewPager2Adapter viewPager2Adapter;
    private int type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .init();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        type = getIntent().getIntExtra("type",1);
        if (type == 1){
            studentHomeFragment = new StudentHomeFragment();
            fragmentList.add(studentHomeFragment);
        }else if (type ==2){
            managerFragment = new ManagerFragment();
            fragmentList.add(managerFragment);
        }else if (type == 3){
            managerFragment = new ManagerFragment();
            fragmentList.add(managerFragment);
        }

        mineFragment = new MineFragment();

        fragmentList.add(mineFragment);
        viewPager2Adapter = new ViewPager2Adapter(this,fragmentList);
        viewPager.setAdapter(viewPager2Adapter);
        radioGroupBottom.check(R.id.radio_home);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(position == 0){
                    radioGroupBottom.check(R.id.radio_home);
                }else {
                    radioGroupBottom.check(R.id.radio_mine);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

        radioGroupBottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.radio_home){
                    viewPager.setCurrentItem(0);
                }else {
                    viewPager.setCurrentItem(1);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}